package com.zzzzzz.content.picitem;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.zzzzzz.common.GlobalConstants;
import com.zzzzzz.content.ContentPictureService;
import com.zzzzzz.content.pictext.PicTextService;
import com.zzzzzz.core.exception.NoSuchModelException;

@Service("PicItemService")
public class PicItemServiceImpl implements PicItemService {
	
	@Resource
	private PicItemDao picItemDao;
	@Resource
	private PicTextService picTextService;
	@Resource
	private ContentPictureService contentPictureService;
	
	@Transactional
	public Long add(Long userId, PicForm picForm) throws IOException {
		Long id = picItemDao.add(picForm.getPicTextId(), userId);
		contentPictureService.savePicItemPicture(id, picForm.getFiles());
		this.editPics(picForm.getPicTextId(), userId);
		return id;
	}
	
	@Transactional
	public void del(Long id, Long userId) throws NoSuchModelException {
		picItemDao.editStatus(id, GlobalConstants.STATUS.DELETED, userId);
		
		PicItem picItem = this.findById(id);
		Long picTextId = picItem.getPicTextId();
		this.editPics(picTextId, userId);
	}
	
	@Transactional
	public Long edit(Long userId, PicItemForm picItemForm) {
		Long picTextId = null;
		LinkedHashMap<Long, String> textSMap = picItemForm.getTextSMap();
		for(Map.Entry<Long, String> entry : textSMap.entrySet()){
			Long id = entry.getKey();
			String textS = entry.getValue();
			Boolean isCover = (id == picItemForm.getCoverId() ? true : false);
			
			if(picTextId == null){
				PicItem picItem = this.findById(id);
				picTextId = picItem.getPicTextId();
			}
			if(isCover){
				this.editIsCoverByPicTextId(false, picTextId, true, userId);
				this.editIsCoverById(id, isCover, userId);
			}
			
			PicItem picItem = new PicItem();
			picItem.setId(id);
			picItem.setTextS(textS);
			picItem.setIsCover(isCover);
			picItem.setUserId(userId);
			picItemDao.edit(picItem);
		}
		
		this.editPics(picTextId, userId);
		
		return picTextId;
	}
	
	public void plusLooks(Long id) {
		picItemDao.editLooks(id, 1);
	}

	public void plusReplies(Long id, Long userId) {
		picItemDao.editReplies(id, 1);
	}

	public void editLikes(Long id, Integer likesPlus) {
		picItemDao.editLikes(id, likesPlus);
	}
	
	private void editIsCoverByPicTextId(Boolean isCoverNew, Long picTextId, Boolean isCoverOld, Long userId){
		picItemDao.editIsCoverByPicTextId(isCoverNew, picTextId, isCoverOld, userId);
	}
	private void editIsCoverById(Long id, Boolean isCover, Long userId){
		picItemDao.editIsCoverById(id, isCover, userId);
	}
	
	public PicItem findById(Long id){
		return picItemDao.findById(id);
	}
	
	public PicItemView findViewById(Long id){
		return picItemDao.findViewById(id);
	}
	
	public List<PicItemListView> findByGtId(Long id){
		PicItem picItem = this.findById(id);
		Long picTextId = picItem.getPicTextId();
		return picItemDao.findByGtId(picTextId, id);
	}
	
	public List<PicItemListView> findByPicTextId(Long picTextId, int start, int offset){
		return picItemDao.findByPicTextId(picTextId, start, offset);
	}
	
	public long findByPicTextIdCount(Long picTextId){
		return picItemDao.findByPicTextIdCount(picTextId);
	}
	
	private String findIdByPicTextId(Long picTextId){
		List<Long> idList = picItemDao.findIdByPicTextId(picTextId, 0, 9);//Get 9 pics
		if(idList.size() > 0){
			String ids = StringUtils.join(idList, ",");
			return ids;
		}
		return null;
	}
	
	public void setPicItemProperty(PicItemView picItemView){
		List<Long> idList = picItemDao.findAllIdByPicTextId(picItemView.getPicTextId());
		int size = idList.size();
		picItemView.setCount(size);

		if(size > 0){
			int idx = idList.indexOf(picItemView.getId());
			picItemView.setIdx(idx + 1);
			
			if(idx == 0){
				picItemView.setPrevId(null);
				picItemView.setNextId(idList.get(idx+1));
			}else if(idx+1 == size){
				picItemView.setPrevId(idList.get(idx-1));
				picItemView.setNextId(null);
			}else{
				picItemView.setPrevId(idList.get(idx-1));
				picItemView.setNextId(idList.get(idx+1));
			}
		}else{
			picItemView.setIdx(0);
			picItemView.setPrevId(null);
			picItemView.setNextId(null);
		}
	}
	
	private void editPics(Long picTextId, Long userId){
		String picIds = this.findIdByPicTextId(picTextId);
		picTextService.editPicIds(picTextId, picIds, userId);
	}
	
	@Transactional
	public void editSeqNb(String ids, Long userId) {
		Iterable<String> idIterable = Splitter.on(',').omitEmptyStrings().trimResults().split(ids);
		List<String> idList = Lists.newArrayList(idIterable);
		for(int seqNb=0; seqNb<idList.size(); seqNb++){
			picItemDao.editSeqNb(seqNb + 1, Long.valueOf(idList.get(seqNb)), userId);
		}
	}
}
